package net.sxnic.ugr.user.action;

/**
 * 
 * Introduction：用户编辑action类
 * 
 * @author 孙宇飞 create date : 2010-5-16
 * @author 吕盛槐 update date : 2011-9-26
 */
import net.sxnic.ugr.UgrActionSupport;
import net.sxnic.ugr.role.Role;
import net.sxnic.ugr.role.util.RoleUtils;
import net.sxnic.ugr.user.User;
import net.sxnic.ugr.user.UserManager;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping(value = "/admin/ugr")
public class Edit extends UgrActionSupport {

	@Autowired
	private UserManager userManager;

	private User user;

	/**
	 * 用户所属部门的code序列
	 */
	private String depts;

	/**
	 * 用户所属角色Html checkbox代码
	 */
	private String rolesHtml;

	@RequestMapping(value = "user_edit.jspa", method = RequestMethod.GET)
	public String execute(String id, User user, int page,ModelMap model)
			throws Exception {

		try {
			checkRoles(Role.SYSTEM_ADMIN);

			user = userManager.get(id);
			if (user == null) {
				addActionError(getText("user.error.notFound"));
				return INPUT;
			}

			depts = StringUtils.removeEnd(depts, ",");

			// rolesHtml
			rolesHtml = RoleUtils.createCheckboxHtml(user.getRoles(),
					authorizationManager.getRoles(), "role");

		} catch (Exception e) {
			e.printStackTrace();
		}
		
		model.addAttribute("user", user);
		model.addAttribute("id", id);
		model.addAttribute("rolesHtml",rolesHtml);
		model.addAttribute("depts", depts);
		model.addAttribute("page", page);
		
		return "admin/ugr/user/edit";
	}

	/**
	 * @return the user
	 */
	public User getUser() {
		return user;
	}

	public String getDepts() {
		return depts;
	}

	public void setDepts(String depts) {
		this.depts = depts;
	}

	public String getRolesHtml() {
		return rolesHtml;
	}

	public void setRolesHtml(String rolesHtml) {
		this.rolesHtml = rolesHtml;
	}

}
